package cn.fl.mcr.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrLogInfoTraceService;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.condition.McrLogTraceQC;
import cn.lionbridgecapital.loan.model.dto.McrLogTraceDTO;
import cn.lionbridgecapital.loan.model.po.McrLogTrace;
import cn.lionbridgecapital.loan.model.utils.DateUtils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: public-loan
 * @ClassName: McrLogTraceController
 * @Description: 日志业务功能
 * @Author: kongdeqiang
 * @Date: 2020/3/10 10:37
 */
@RestController
@RequestMapping("/mcr/mcrLogTrace")
public class McrLogTraceController {
    private static final Logger logger = LoggerFactory.getLogger(McrLogTraceController.class);
    @Autowired
    private IMcrLogInfoTraceService mcrLogInfoTraceService;


    /**
     * 到列表页面
     */
    @RequestMapping(value = "/logsList.do")
    public ModelAndView logsList(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("mcr/mcrLogTrace/mcrLogTraceList");
        BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }
    /**
     * 到列表页面
//     */
    @RequestMapping(value = "/initList.do" ,method = RequestMethod.POST)
    public Object initList(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (cn.cmm.base.framework.utils.StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (cn.cmm.base.framework.utils.StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        McrLogTraceQC qc = new McrLogTraceQC();

        String cstId = request.getParameter("cstId");
        if(StringUtils.isNotEmpty(cstId)) {
            qc.setCstId(cstId);
        }
        String userId = request.getParameter("userId");
        if(StringUtils.isNotEmpty(userId)) {
            qc.setUserId(userId);
        }

        String mobileNo = request.getParameter("mobileNo");
        if(StringUtils.isNotEmpty(mobileNo)) {
            qc.setMobileNo(mobileNo);
        }
        String cstName = request.getParameter("cstName");
        if(StringUtils.isNotEmpty(cstName)) {
            qc.setCstName(cstName);
        }

        String logStatus = request.getParameter("logStatus");
        if(StringUtils.isNotEmpty(logStatus)) {
            qc.setLogStatus(Integer.parseInt(logStatus));
        }


        String crtTmStart = request.getParameter("crtTmStart");
        if(StringUtils.isNotEmpty(crtTmStart)) {
            if(crtTmStart.length()>=10) {
                crtTmStart = crtTmStart + " 00:00:00";
            }
            qc.setCrtTmStart(DateUtils.formatStringToDate(crtTmStart,DateUtils.YYYY_MM_DDHHMMSS));
        }
        String crtTmEnd = request.getParameter("crtTmEnd");
        if(StringUtils.isNotEmpty(crtTmEnd)) {
            if(crtTmEnd.length()>=10) {
                crtTmEnd = crtTmEnd + " 23:59:59";
            }
            qc.setCrtTmEnd(DateUtils.formatStringToDate(crtTmEnd,DateUtils.YYYY_MM_DDHHMMSS));
        }
        page.setCondition(qc);
        //TODO SOMETHING
        logger.info("输入参数:"+JSONObject.toJSONString(page));
        BaseLogUtils.info(logger,"initList","输入参数:"+JSONObject.toJSONString(page));
        Pager pager;
        try {
            pager = mcrLogInfoTraceService.searchListPage(page);
        }catch(Exception e){
            return null;
        }
        List<McrLogTrace> list = (List<McrLogTrace>) pager.getResultList();
        ListPageVO<McrLogTrace> listPage = new ListPageVO<McrLogTrace>(list, pager.getTotalCount());
        logger.info("查询到的数据:"+JSONObject.toJSONString(listPage));
        BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        return listPage;
    }



}
